
//引入插件：微信同声传译
const plugin = requirePlugin('WechatSI');
//获取全局唯一的语音识别管理器recordRecoManager
const manager = plugin.getRecordRecognitionManager();
 
Page({
  data: { 
    content:'',//听写内容
    id:0,
    title:'题目',
    description:'描述',
    listen_hidden:false,//内容if隐藏
    grade:0,
    color:'#707070',
    startListen:false,
    recordState: false, //录音状态
    text:'',
    isEnd:true,
    contentInput:'',
    isFinish:false,
    status:0
    },
    toworkReport(){
      var content=this.data.content;
      var Title=this.data.title;
      var id=this.data.id;
      wx.reLaunch({
        url: '/pages/workRaport/workRaport?title='+Title+'&id='+id+'&type='+2,
      })
     },
   toRaport(){
    var that=this;
    var content=this.data.contentInput;
    var Title=that.data.title;
    var id=that.data.id;
    console.log(content);
    wx.request({
      url: 'https://www.beitingok.cn:8082/Report/submitAnswer/'+id,
      data: {
        'studentAnswer':content,
      },
      method: 'POST', 
      success: function (res) {
        console.log(res);
        if(res.data.success==true){
        wx.showToast({
          title: '提交成功',
          icon:'success',
          duration:2000,
          success: function(res) {
            wx.showModal({
              title: '提示',
              content:'提交成功',
              showCancel:false
            })
           wx.reLaunch({
             url: '/pages/workRaport/workRaport?title='+Title+'&id='+id+'&content='+content,
           })
          }
        })
      }
      },
      fail: function (res) {
       wx.showModal({
        title: '提交失败',
        content:res.data.message,
        showCancel:false
      })
        console.log('cuowu' + ':' + res)
      }
    })
   },

 toStop(){
  this.setData({
    recordState: false,
    isEnd:true,
    isFinish:true
  })
  // 语音结束识别
  manager.stop();
},
 
//  开始识别
 toStart() {
  wx.enableAlertBeforeUnload({
    message: "作业正在进行，是否要退出",
    success: function (res) {
      console.log("方法注册成功:", res);
    },
    fail: function (errMsg) {
      console.log("方法注册失败:", errMsg);
    },
  });
  this.setData({
    startListen:true,
    recordState: true,  //录音状态
    isEnd:false
  })
  // 语音开始识别
  manager.start({
    lang: 'zh_CN',// 识别的语言，目前支持zh_CN en_US zh_HK sichuanhua
    duration:29000
  })
},
 write:function(e) {
   this.setData({
     contentInput:e.detail.value
   })
 },
 /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      this.initRecord();
    this.setData({
      id: options.id,
      description:options.description,
      title:options.title,
      content: options.content,
      status:options.status
    })
    // console.log(options.id);
    // console.log(this.data.id);
    // console.log(options.content);
    // console.log(options.description);
    // console.log(options.title);
    var id=this.data.id;
    // console.log(id);
    var that=this;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  // 初始化事件
  initRecord: function () {
    const that = this;
    // 有新的识别内容返回，则会调用此事件
    manager.onRecognize = function (res) {
      console.log(res)
      that.setData({
       text:res.result
      })
    }
    // 正常开始录音识别时会调用此事件
    manager.onStart = function (res) {
      console.log("成功开始录音识别", res)
    }
    // 识别错误事件
    manager.onError = function (res) {
      console.error("error msg", res)
      if (res.result == '') {
        wx.showModal({
          title: '提示',
          content: '听不清楚，请重新说一遍！',
          showCancel: false,
          success: function (res) {}
        })
        return;
      }
      var text = that.data.contentInput+ res.result;
      that.setData({
        contentInput: text,
        text:""
      })
      if(that.data.isEnd==false){
        that.toStart()
        }

    }
    //识别结束事件
    manager.onStop = function (res) {
      console.log('..............结束录音')
      console.log('录音临时文件地址 -->' + res.tempFilePath); 
      console.log('录音总时长 -->' + res.duration + 'ms'); 
      console.log('文件大小 --> ' + res.fileSize + 'B');
      console.log('语音内容 --> ' + res.result);
      if (res.result == '') {
        if(that.data.isEnd==true){
        wx.showModal({
          title: '提示',
          content: '听不清楚，请重新说一遍！',
          showCancel: false
        })
      }
        if(that.data.isEnd==false){
          that.toStart()
          }
        return;
      }
      var text = that.data.contentInput + res.result;
      that.setData({
        contentInput: text,
        text:""
      })
      if(that.data.isEnd==false){
        that.toStart()
        }
    }
  }

})